﻿using System.Collections.Generic;
using Game2DFramework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechGame.Game.Particles
{
    class ExplosionManager : GameObject
    {
        private readonly Texture2D _explosionTexture;
        private readonly List<ExplosionInstance> _explosions; 
        
        public ExplosionManager(Game2D game) : base(game)
        {
            _explosionTexture = Game.Content.Load<Texture2D>("Textures/explosion");
            _explosions = new List<ExplosionInstance>();
        }

        public void SpawnExplosion(Vector2 position)
        {
            var explosion = new ExplosionInstance(_explosionTexture, position);
            explosion.Play();
            _explosions.Add(explosion);
        }
        
        public void Update(float elapsedTime)
        {
            _explosions.ForEach(e => e.Update(elapsedTime));
            _explosions.RemoveAll(e => !e.Running);
        }

        public void Draw()
        {
            _explosions.ForEach(e => e.DrawExplosion(Game.SpriteBatch));
        }
    }
}
